#############
## build ##
#############
FROM node:18-alpine as builder

WORKDIR /build

# 复用 node_modules 缓存
COPY ./package.json ./
RUN npm config set registry https://registry.npmmirror.com && npm install

COPY . .
# 这行构建命令根据具体项目调整 #
RUN npm run build

#############
## release ##
#############
FROM nginx:alpine

WORKDIR /app
EXPOSE 80

# 内嵌文本, 其中的变量需要转义
COPY <<EOF /etc/nginx/templates/default.conf.template
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /app/dist/;
        index  index.html index.htm;
        if (!-e \$request_filename) {
            rewrite ^/(.*) /index.html last;
            break;
        }
        autoindex on;
    }
}
EOF

COPY --from=builder /build/dist /app/dist

CMD ["nginx", "-g", "daemon off;"]
